package my1024.model;

import java.awt.event.KeyEvent;

import javax.vecmath.Point2d;

import my1024.app.Const;

/**
 * minhvv, p18, IFI, TPE 01/2014 - 07/2014
 */

public class BoxModel {
	private int id;
	private int value;
	private int logicRow;
	private int logicCol;

	public BoxModel(int id, int value, int logicRow, int logicCol) {
		this.id = id;
		this.value = value;
		this.logicRow = logicRow;
		this.logicCol = logicCol;
	}

	public boolean matchId(BoxModel otherModel) {
		return this.id == otherModel.id;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public Point2d getRenderLocation() {
		return new Point2d(colToX(logicCol), rowToY(logicRow));
	}

	private int rowToY(int row) {
		return Const.MAX_ROW / 2 - 1 - row;
	}

	private int colToX(int col) {
		return col - Const.MAX_COL / 2;
	}

	public void correctPosition(int keyCode) {
		int oldRow = logicRow;
		int oldCol = logicCol;

		switch (keyCode) {
		case KeyEvent.VK_LEFT:
			logicCol = Const.MAX_COL - 1 - oldCol;
		case KeyEvent.VK_RIGHT:
			// do nothing
			break;
		case KeyEvent.VK_UP:
			logicRow = Const.MAX_COL - 1 - oldCol;
			logicCol = oldRow;
			// logicRow = oldCol;
			// logicCol = Const.MAX_ROW - 1 - oldRow;
			break;
		case KeyEvent.VK_DOWN:
			// logicRow = Const.MAX_COL - 1 - oldCol;
			// logicCol = oldRow;
			logicRow = oldCol;
			logicCol = Const.MAX_ROW - 1 - oldRow;
			break;
		default:
			break;
		}
	}

	public String toString() {
		StringBuilder sb = new StringBuilder("BoxModel{data:[");
		sb.append("id:").append(id);
		sb.append(",v:").append(value);
		sb.append(",r:").append(logicRow);
		sb.append(",c:").append(logicCol);
		sb.append("],renderPos:[");
		sb.append(getRenderLocation().toString());
		sb.append("}");
		return sb.toString();
	}

}
